module PrintOrder

  def generate_content order
    content = '<CB>测试打印</CB><BR>'
    content += "订单号:  #{order.order_number}<BR>"
    content += '名称　　　　　 单价  数量 金额<BR>'
    content += '--------------------------------<BR>'
    order.order_items.each do |line_item|
      content += "#{line_item.food_name}　　　　　 　#{line_item.single_price}   #{line_item.quantity}  #{line_item.total_price}<BR>"    
      content += '--------------------------------<BR>'
    end
    content += "合计：#{order.total_price}元<BR>"
    content += '<BR>'
    content += "<C><QR>#{HOST}/weixin/welcome/#{order.restaurant.id}/#{order.table_no}</QR></C>"
    content += "<PLUGIN>您有新的订单</PLUGIN>"
    content += "<CUT>"
    content
  end  

  def printers_print restaurant,content
    restaurant.printers.each do |printer|
      reprint_order printer.sn,printer.key,content
    end  
  end  

  def reprint_order sn,key,content
        params = {}
        params["sn"] = sn  
        params["content"] = content #打印内容 
        params["times"] = "1" #打印联数 
        params["apiname"] = "Open_printMsg"
        stime = Time.now.to_i
        params['stime'] = stime
        params['user'] = 'printer@guikpay.com'
        key = Digest::SHA1.hexdigest "printer@guikpay.comsMqu8aW5WmYv888T#{stime}"
        params["sig"] = key
        uri = URI.parse("http://api.feieyun.cn/Api/Open/")
        res = Net::HTTP.post_form(uri, params)
        puts res.body
    end

end  